package com.ruoyi.business.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.BusPerson;
import com.ruoyi.business.service.IBusPersonService;
import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.BusTeam;
import com.ruoyi.business.service.IBusTeamService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 班组管理Controller
 * 
 * @author wf
 * @date 2024-07-10
 */
@RestController
@RequestMapping("/business/team")
public class BusTeamController extends BaseController
{
    @Autowired
    private IBusTeamService busTeamService;

    @Autowired
    private IBusPersonService busPersonService;
    @Value("${export.max:10000}")
    private Integer max;

    /**
     * 查询班组管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:team:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusTeam busTeam)
    {
        startPage();
        List<BusTeam> list = busTeamService.selectBusTeamList(busTeam);
        return getDataTable(list);
    }

    //  查询班组集合
    @GetMapping("/teamlist")
    public TableDataInfo TeamList()
    {
        List<BusTeam> list = busTeamService.selectBusGroupTeamList();
//        list.forEach(System.out::println);
        return getDataTable(list);
    }

    /**
     * 导出班组管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:team:export')")
    @Log(title = "班组管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusTeam busTeam)
    {
        List<BusTeam> list = busTeamService.selectBusTeamList(busTeam);
        if (list.size() > max) {
            throw new ServiceException("导出失败！,每次最多导出 " + max + "条");
        }
        ExcelUtil<BusTeam> util = new ExcelUtil<BusTeam>(BusTeam.class);
        util.exportExcel(response, list, "班组管理数据");
    }

    /**
     * 获取班组管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:team:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(busTeamService.selectBusTeamById(id));
    }

    /**
     * 新增班组管理
     */
    @PreAuthorize("@ss.hasPermi('business:team:add')")
    @Log(title = "班组管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusTeam busTeam)
    {
        return toAjax(busTeamService.insertBusTeam(busTeam));
    }

    /**
     * 修改班组管理
     */
    @PreAuthorize("@ss.hasPermi('business:team:edit')")
    @Log(title = "班组管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusTeam busTeam)
    {
        return toAjax(busTeamService.updateBusTeam(busTeam));
    }

    /**
     * 删除班组管理
     */
    @PreAuthorize("@ss.hasPermi('business:team:remove')")
    @Log(title = "班组管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(busTeamService.deleteBusTeamByIds(ids));
    }


    /**
     * 功能描述：检测该班组管理是否被选择
     *
     * @param ids
     * @return int
     * @author ling
     * @date 2024/07/11 09:16
     */
    @PreAuthorize("@ss.hasPermi('business:team:check')")
    @PostMapping ("/check/{ids}")
    public AjaxResult check(@PathVariable Long[] ids)
    {
        System.out.println("参数： "+ Arrays.toString(ids));
        List<BusPerson> bp = busPersonService.checkBusTeamById(ids);
        System.out.println("人员查询结果：");
//        bp.forEach(System.out::println);
        Integer peopleNum = null;
        bp.stream().forEach(s-> System.out.println(s.getPeopleNum()));
        for (BusPerson b:bp
             ) {
          peopleNum =  b.getPeopleNum();
        }
        // 判断 人数是否为null
        if (peopleNum == null){

            return success(peopleNum);
        }else {
            return success(peopleNum);
        }
    }




}
